<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-06 09:25
 *
 * 项目：rm  -  $  - ListController.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\controllers;

use Lev;
use lev\base\Assetsv;
use lev\base\Controllerv;
use lev\base\Viewv;
use modules\levfm\table\LevfmDatasChild;
use modules\levfm\table\LevfmFields;
use modules\levfm\table\LevfmForms;

!defined('INLEV') && exit('Access Denied LEV');


class ListController extends Controllerv
{
    public static $formInfo = null;

    public static function actionIndex($param = []) {
        Viewv::render('views/list/index', $param ?: static::viewParam());
    }

    public static function formInfo() {
        $formid = Lev::GETv('opid');

        $formid = floatval($formid);

        static::$formInfo === null &&
        static::$formInfo = $formid <1 ? [] : LevfmForms::findOne(['id'=>$formid]);

        $formid && $_GET['form'] = $formid;
        return static::$formInfo;
    }

    public static function viewParam() {
        $formInfo = static::formInfo();
        $formid   = $formInfo['id'];

        $uid = floatval(Lev::GPv('uid'));
        $_srhTypes = Lev::stripTags($_GET);

        $srhTypes = [];

        $srhkey = Lev::stripTags(Lev::GETv('srhkey'));

        Lev::$app['title'] = '表单数据列表';
        if ($formInfo) {
            $inputs = LevfmFields::getFormFields($formid);

            $formInfo['settings'] = Lev::getSettings($formInfo['settings']);

            $typeSearchs = LevfmForms::getSettingsTypeSearchsToId($formInfo['settings'], $inputs) ?: [];
            $typeSearchs+= LevfmForms::getDayTypeSearchs($formInfo);

            if ($_srhTypes && $typeSearchs) {
                foreach ($typeSearchs as $v) {
                    $idk = is_array($v) ? $v['id'] : $v;
                    isset($_srhTypes[$idk]) && $srhTypes[$idk] = $_srhTypes[$idk];
                }
            }

            Lev::$app['title'] = $formInfo['name'] . ' - 表单数据列表';
            Lev::$app['metakeyword'] = $formInfo['name'] . ',' . Lev::$app['metakeyword'];
            Lev::$app['metadesc'] = $formInfo['name'] . '，' . $formInfo['descs'] . Lev::$app['metadesc'];

            $formInfo['_listSet'] = LevfmForms::getSettingsFormLists($formInfo['settings']);
        }else {
            $formInfo['_listSet'] = ['titlePre'=>'', 'titleSuf'=>''];
        }

        $where = [];
        if (is_numeric($day  = Lev::stripTags(Lev::GETv('day')))) {//查询一天内所有数据
            $daytime = LevfmForms::setDayTime(-$day);
            $endtime = $daytime + 24 * 3600;
            $where[] = "uptime<{$endtime}";
            $where['uptime'] = "uptime>={$daytime}";
        }
        if (is_numeric($days  = Lev::stripTags(Lev::GETv('days')))) {//查询几天前所有数据
            $daytime = LevfmForms::setDayTime(-$days);
            $where['uptime'] = "uptime>={$daytime}";
        }
        if ($srhkey) {
            $srhkey = addcslashes($srhkey, '%_');
            $where[] = "srhkey LIKE '#%{$srhkey}%'";
        }else if ($srhTypes) {
            foreach ($srhTypes as $_fieldid => $value) {
                if (is_numeric($_fieldid)) {
                    $_srhkey = addcslashes(LevfmDatasChild::setSrhString($_fieldid, $value), '%_');
                    $where[] = "srhtype LIKE '[%$_srhkey%'";
                    //$where[] = "POSITION('$_srhkey' IN `srhtype`)";
                }
            }
        }
        $formid && $where[] = "formid=$formid";
        $uid >0 && $where[] = "uid=$uid";

        $where = implode(' AND ', $where);
        $limit = LevfmForms::getSettingsPageNum($formInfo['settings']);
        $order = ['id DESC'];
        $field = 'id,uid,formid,srhkey,uptime';
        $lists = LevfmDatasChild::pageButtons($where?:1, $limit, $order, 5, '', $field);

        Assetsv::animateCss();
        return [
            'lists'       => $lists,
            'formid'      => $formid,
            'srhTypes'    => $srhTypes,
            'inputs'      => $inputs,
            'typeSearchs' => $typeSearchs,
            'srhkey'      => $srhkey,
            'formInfo'    => $formInfo,
        ];
    }

}